//
//  FamilyInformationViewController.m
//  TenThousand
//
//  Created by 我的 on 16/7/7.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "FamilyInformationViewController.h"
#import "PersonTableViewCell.h"
#import "PersonalDataViewController.h"

@interface FamilyInformationViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *_getText;
    NSMutableDictionary *_famDic;
}

@property (nonatomic, strong) NSMutableArray *userInfoClassArrM;
@property (nonatomic, assign) NSInteger houseInfo;

@end
#define PERSONALARR @[@"住房信息",@"婚姻情况"]
#define INFORMATION @[@"别墅",@"已婚"]
#define FamilyInfo @[@"Housing",@"Marriage"]
@implementation FamilyInformationViewController

- (NSMutableArray *)userInfoClassArrM {
    if (!_userInfoClassArrM) {
        _userInfoClassArrM = [NSMutableArray array];
    }
    return _userInfoClassArrM;
}

- (NSMutableDictionary *)userInfoDicM {
    if (!_userInfoDicM) {
        _userInfoDicM = [NSMutableDictionary dictionary];
    }
    return _userInfoDicM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置家庭信息";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
    [self.view addSubview:self.familyTableView];
    _famDic = [NSMutableDictionary dictionary];
    
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick:)];
}

- (void)getUserInfoClassDate:(NSString *)typeId {
    /*类型 TypeId
     0.工作情况 1.教育情况  2.企业性质  3.行业分类 4.住房情况*/
    NSDictionary *dict1 = @{
                            @"TypeId":typeId
                            };
    //用户基础信息资料读取接口
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"User.GetUserInfoClass" withParaments:dict1 withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        [self.userInfoClassArrM removeAllObjects];
        [self.userInfoClassArrM addObjectsFromArray:object[@"Data"]];
        [self showPickerView];
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
    
}

- (void)showPickerView {
    NSMutableArray *arrM = [NSMutableArray array];
    for (NSDictionary *dic in self.userInfoClassArrM) {
        [arrM addObject:dic[@"InfoName"]];
    }
    
//    NSMutableArray *arr = (NSMutableArray *)@[@"别墅",@"花园洋房",@"公寓式住宅",@"普通住宅",@"家庭自有住房",@"借住"];
    BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arrM WithIndex:1];
    
    //取值
    pickerView.clickedBtnIndexBlock = ^(NSString *text, NSInteger index){
        _getText = text;
        if (index == 0) {
            self.houseInfo = index + 1;
        } else {
            self.houseInfo = index + 3;
        }
        [_famDic setValue:[NSString stringWithFormat:@"%ld", self.houseInfo] forKey:@"houseInfoNum"];
        [_famDic setValue:_getText forKey:@"houseInfo"];
        NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:0];//刷新第二个section
        [_familyTableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
    };
    [pickerView showView];

}

- (void)rightButtonClick:(UIBarButtonItem *)item{
    
    if (self.ReturnDicBlock != nil) {
        self.ReturnDicBlock(_famDic);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.ReturnDicBlock != nil) {
        self.ReturnDicBlock(_famDic);
    }
}


- (UITableView *)familyTableView{
    if (!_familyTableView) {
        _familyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStyleGrouped];
        _familyTableView.delegate = self;
        _familyTableView.dataSource = self;
        _familyTableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
        _familyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.familyTableView.showsVerticalScrollIndicator = NO;
        _familyTableView.scrollEnabled = NO;
    }
    return _familyTableView;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonTableViewCell *personalCell = [tableView dequeueReusableCellWithIdentifier:@"PERSONALTABLEVIEWCELL"];
    if (personalCell == nil) {
        personalCell = [[[NSBundle mainBundle] loadNibNamed:@"PersonTableViewCell" owner:nil options:nil] lastObject];
    }
    personalCell.selectionStyle = UITableViewCellSelectionStyleNone;
    personalCell.informationLable.text = PERSONALARR[indexPath.section];
    
    //设置向右的箭头
    
    
    personalCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    personalCell.inforTextfield.enabled = NO;
    if ([[self.userInfoDicM objectForKey:FamilyInfo[indexPath.section]] isEqualToString:@""]) {
        personalCell.inforTextfield.placeholder = @"请选择";
    } else {
        personalCell.inforTextfield.placeholder = [self.userInfoDicM objectForKey:FamilyInfo[indexPath.section]];
    }
    
    personalCell.inforTextfield.text = _getText;
    return personalCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0 ) {
        return 15;
    }else{
        return 8;
    }
    return 0.01;
}


//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            [self getUserInfoClassDate:@"4"];
        }
            break;
            
        case 1:
        {
            NSArray *arr = @[@"请选择", @"未婚", @"已婚"];
            BXPickerView *pickerView = [[BXPickerView alloc] initWithFrame:CGRectMake(0, Screen_Height-220, Screen_Width, 220) WithTitleListArr:arr WithIndex:1];
            pickerView.clickedBtnIndexBlock = ^(NSString *text, NSInteger index){
                _getText = text;
                [_famDic setValue:_getText forKey:@"marriage"];//婚姻情况
                NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:1];//刷新第二个section
                [_familyTableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
            };
            
            [pickerView showView];
            
        }
            break;
        default:
            break;
    }
}



@end
